import ErrorPage from '@/components/error-page'
import PullTo from 'vue-pull-to'
// import pageList from '@/components/common-js/page-list.vue'

export default {
  name: 'page-list',
  components: { ErrorPage, PullTo },
  data () {
    return {
      page: {
        url: '', // 缓存上次的url
        list: [], // 加载的数据列表
        more: true, // 是否有更多
        curpage: 0, // 当前数据页数,从0开始
        pagesize: 10, // 每页条数,默认为10
        loading: false,
        loaded: false
      },
      searchData: {
      },
      mocklist: null
    }
  },
  // components: { pageList },
  methods: {
    async $more (loaded) {
      if (this.page.loading === false) {
        try {
          await this.loadPageList()
        } finally {
          loaded('done')
        }
      }
    },
    // 1.查询的url
    // 2.刷新,清除掉之前的查询结果
    async loadPageList (url, reset) {
      if (this.page.loading) {
        // 上次查询还没有结束时,不继续查询
        return
      }
      if (reset) {
        this.page.loaded = false
        this.page.more = true
        this.page.curpage = 0
        if (this.page.list.length > 0) {
          this.page.list.splice(0, this.page.list.length)
        }
      }
      this.page.url = url || this.page.url
      if (!this.page.more) {
        // 没有更多的数据时,不继续查询
        return
      }
      this.page.loading = true
      let list = await this.$ajax.get(this.page.url, { curpage: this.page.curpage, pagesize: this.page.pagesize, ...this.searchData })
      this.page.list = this.page.list.concat(list)
      if (this.mocklist) {
        this.page.list = this.page.list.concat(this.mocklist)
      }
      if (list.length < this.page.pagesize) {
        this.page.more = false// 是否有更多数据,默认为有
      } else {
        this.page.curpage += 1
      }
      this.page.loading = false
      this.page.loaded = true
    }
  }
}
